//
//  DataOperationLayer.m
//  SimpleNote
//
//  Created by mac on 2017/11/24.
//  Copyright © 2017年 上海LOL信息技术服务有限公司. All rights reserved.
//

#import "DataOperationLayer.h"
#import "BookTable.h"

@interface DataOperationLayer ()

@property (nonatomic,strong) BookTable *booktable;

@end


@implementation DataOperationLayer

+(DataOperationLayer *)shareInstance{
    static DataOperationLayer *_layer = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _layer = [[DataOperationLayer alloc]init];
    });
    return _layer;
}

-(instancetype)init{
    if (self = [super init]) {
        self.booktable = [[BookTable alloc]init];
    }
    return self;
}

-(void)insertBookRecord:(BookRecord *)model ResultBlock:(ResultBlock)resultBlock{
    NSError *error = nil;
    BOOL issuccess = [self.booktable insertRecord:model error:&error];
    resultBlock(issuccess,error);
}


-(NSArray *)selectAllBookRecord{
//     Select <列的集合> from <表名> where <条件> order by <排序字段和方式>
    NSError *error = nil;
    NSArray *tempArray;
    tempArray = [self.booktable fetchWithSQL:@"SELECT * FROM BookTable order by time desc" error:&error];
    NSMutableArray *finalArray = [BookRecord mj_objectArrayWithKeyValuesArray:tempArray];
    return [finalArray copy];
    
//    // NSArray --> NSMutableArray
//    NSMutableArray *myMutableArray = [myArray mutableCopy];
//
//    // NSMutableArray --> NSArray
//    NSArray *myArray = [myMutableArray copy]; 
}

-(void)updateBookRecord:(BookRecord *)model{
    NSError *error = nil;
    [self.booktable updateRecord:model error:&error];
}

-(void)deleteBookRecord:(BookRecord *)model{
    NSError *error = nil;
    [self.booktable deleteRecord:model error:&error];
}


@end



